<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="numpy," />





  <link rel="alternate" href="/blog/atom.xml" title="稻草人的编程之路" type="application/atom+xml" />






<meta name="description" content="NumPyNumPy（Numeric Python）是一个用于科学计算的基础包。NumPy包含：  处理强大的多维数组对象的能力 广播功能函数 整个c/c++与Fortran代码的工具 线性代数、傅里叶变换、随机数生成的能力  本教程只涉及一些常用的操作，详细用法请查看 NumPy官方文档 基础对于一个python的数组，如果要让每个元素的值都加1，该如何实现呢？ 123456789&amp;gt;&amp;gt">
<meta name="keywords" content="numpy">
<meta property="og:type" content="article">
<meta property="og:title" content="数据分析之numpy">
<meta property="og:url" content="https://wangxiaochuang.github.io/2018/09/15/1.html">
<meta property="og:site_name" content="稻草人的编程之路">
<meta property="og:description" content="NumPyNumPy（Numeric Python）是一个用于科学计算的基础包。NumPy包含：  处理强大的多维数组对象的能力 广播功能函数 整个c/c++与Fortran代码的工具 线性代数、傅里叶变换、随机数生成的能力  本教程只涉及一些常用的操作，详细用法请查看 NumPy官方文档 基础对于一个python的数组，如果要让每个元素的值都加1，该如何实现呢？ 123456789&amp;gt;&amp;gt">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-07T22:36:29.352Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数据分析之numpy">
<meta name="twitter:description" content="NumPyNumPy（Numeric Python）是一个用于科学计算的基础包。NumPy包含：  处理强大的多维数组对象的能力 广播功能函数 整个c/c++与Fortran代码的工具 线性代数、傅里叶变换、随机数生成的能力  本教程只涉及一些常用的操作，详细用法请查看 NumPy官方文档 基础对于一个python的数组，如果要让每个元素的值都加1，该如何实现呢？ 123456789&amp;gt;&amp;gt">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://wangxiaochuang.github.io/2018/09/15/1.html"/>





  <title>数据分析之numpy | 稻草人的编程之路</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">稻草人的编程之路</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">众人皆醉我独醒 举世皆浊我独清</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/09/15/1.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">数据分析之numpy</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-15T00:05:36+00:00">
                2018-09-15
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/" itemprop="url" rel="index">
                    <span itemprop="name">数据分析</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/基础工具/" itemprop="url" rel="index">
                    <span itemprop="name">基础工具</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/09/15/1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/09/15/1.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/09/15/1.html" class="leancloud_visitors" data-flag-title="数据分析之numpy">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="NumPy"><a href="#NumPy" class="headerlink" title="NumPy"></a>NumPy</h1><p>NumPy（Numeric Python）是一个用于科学计算的基础包。NumPy包含：</p>
<ul>
<li>处理强大的多维数组对象的能力</li>
<li>广播功能函数</li>
<li>整个c/c++与Fortran代码的工具</li>
<li>线性代数、傅里叶变换、随机数生成的能力</li>
</ul>
<p>本教程只涉及一些常用的操作，详细用法请查看 <a href="http://www.numpy.org/">NumPy官方文档</a></p>
<h2 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h2><p>对于一个python的数组，如果要让每个元素的值都加1，该如何实现呢？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = [<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>,<span class="number">13</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> idx, element <span class="keyword">in</span> enumerate(arr):</span><br><span class="line"><span class="meta">... </span>    arr[idx] += <span class="number">1</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">[<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>]</span><br><span class="line"><span class="comment"># 使用高级python语法，列表推导，或许会更直观些</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[element + <span class="number">1</span> <span class="keyword">for</span> element <span class="keyword">in</span> arr]</span><br><span class="line">[<span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>]</span><br></pre></td></tr></table></figure>
<p>使用NumPy，我们可以更方便的操作，不需要各种循环以及高级语法，本来就是一件很简单的事情</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>,<span class="number">13</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr + <span class="number">1</span></span><br><span class="line">array([<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>])</span><br></pre></td></tr></table></figure>
<p>使用<code>np.array</code>包裹上一个数组，就构造了一个<code>ndarray</code>结构的数组了。使用<code>type</code>函数查看这个<code>arr</code>的类型可以得到结果<code>numpy.ndarray</code></p>
<p>NumPy正是使用了这个类为矩阵运算提供了超强的便捷性</p>
<h3 id="ndarray常用属性"><a href="#ndarray常用属性" class="headerlink" title="ndarray常用属性"></a>ndarray常用属性</h3><p>ndarray结构的一些常用属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">1. ndarray.ndim</span><br><span class="line">    数组的维度数量</span><br><span class="line">2. ndarray.shape</span><br><span class="line">    数组的纬数，将返回一个元组来表示维度信息</span><br><span class="line">3. ndarray.size</span><br><span class="line">    数组包含的元素个数</span><br><span class="line">4. ndarray.dtype</span><br><span class="line">    返回数组每个元素的类型。我们在创建的时候可以指定类型，例如 numpy.int32, numpy.int16, numpy.float64等</span><br><span class="line">5. ndarray.itemsize</span><br><span class="line">    返回数组元素所占用的字节大小，例如 float64占8个字节</span><br><span class="line">6. ndarray.nbytes</span><br><span class="line">    返回数组占用的空间，其值等于 ndarray.size * ndarray.itemsize</span><br><span class="line">7. ndarray.flat</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">ndarray属性例子</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = np.arange(<span class="number">15</span>).reshape(<span class="number">3</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">array([[ <span class="number">0</span>,  <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>,  <span class="number">4</span>],</span><br><span class="line">       [ <span class="number">5</span>,  <span class="number">6</span>,  <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.ndim</span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.shape</span><br><span class="line">(<span class="number">3</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.shape = <span class="number">5</span>, <span class="number">3</span>      <span class="comment"># 我们可以直接修改shape值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">array([[ <span class="number">0</span>,  <span class="number">1</span>,  <span class="number">2</span>],</span><br><span class="line">       [ <span class="number">3</span>,  <span class="number">4</span>,  <span class="number">5</span>],</span><br><span class="line">       [ <span class="number">6</span>,  <span class="number">7</span>,  <span class="number">8</span>],</span><br><span class="line">       [ <span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>],</span><br><span class="line">       [<span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.size</span><br><span class="line"><span class="number">15</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.dtype.name</span><br><span class="line"><span class="string">'int64'</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.itemsize</span><br><span class="line"><span class="number">8</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.nbytes</span><br><span class="line"><span class="number">120</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[item <span class="keyword">for</span> item <span class="keyword">in</span> a.flat]                       <span class="comment"># 将多维数组拉平</span></span><br><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>]</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="ndarray常用方法"><a href="#ndarray常用方法" class="headerlink" title="ndarray常用方法"></a>ndarray常用方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1. ndarray.fill</span><br><span class="line">    使用指定值填充数组</span><br><span class="line">2. ndarray.copy</span><br><span class="line">    复制数组</span><br><span class="line">3. ndarray.astype</span><br><span class="line">    转换类型</span><br><span class="line">4. ndarray.flatten</span><br><span class="line">    转换为平坦的数组</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">ndarray方法例子</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">0.9455</span>, <span class="number">0.102345</span>, <span class="number">0.11234</span>, <span class="number">0.1298</span>, <span class="number">0.13234</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.fill(<span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([ <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr = arr.copy()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr <span class="keyword">is</span> arr</span><br><span class="line"><span class="keyword">False</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.dtype</span><br><span class="line">dtype(<span class="string">'float64'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr = newarr.astype(np.float32)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.dtype</span><br><span class="line">dtype(<span class="string">'float32'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.flatten()</span><br><span class="line">array([ <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>])</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="创建ndarray数组"><a href="#创建ndarray数组" class="headerlink" title="创建ndarray数组"></a>创建ndarray数组</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">1. numpy.array</span><br><span class="line">    传入列表或元组，也可以指定类型</span><br><span class="line">2. numpy.arange</span><br><span class="line">    生成指定步长的等差数组</span><br><span class="line">3. numpy.linspace</span><br><span class="line">    生成指定最大最小值和元素个数的等差数组</span><br><span class="line">4. numpy.logspace</span><br><span class="line">    生成指定最大最小值和元素个数的等比数组</span><br><span class="line">5. numpy.meshgrid</span><br><span class="line">    指定特定维度数据，创建网格数组</span><br><span class="line">6. numpy.r_</span><br><span class="line">    生成行向量</span><br><span class="line">7. numpy.c_</span><br><span class="line">    生成列向量</span><br><span class="line">8. numpy.zeros</span><br><span class="line">    生成指定shape的值为0矩阵</span><br><span class="line">9. numpy.ones</span><br><span class="line">    生成指定shape的值为1的矩阵</span><br><span class="line">10. numpy.ones_like</span><br><span class="line">    生成与指定矩阵同shape的矩阵</span><br><span class="line">11. numpy.empty</span><br><span class="line">    生成指定shape的空矩阵，初始数据是内存的脏数据</span><br><span class="line">12. numpy.identity</span><br><span class="line">    生成指定shape的单位矩阵</span><br><span class="line">13. numpy.random</span><br><span class="line">    利用random模块生成一个随机的mask数组（值为bool类型，用于mask）</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">创建ndarray数组</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.array([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]).shape</span><br><span class="line">(<span class="number">3</span>,)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.array([[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]]).shape</span><br><span class="line">(<span class="number">1</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.arange(<span class="number">5</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.arange(<span class="number">0</span>, <span class="number">5</span>, <span class="number">1</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.arange(<span class="number">0</span>, <span class="number">5</span>, <span class="number">1</span>, dtype=np.int16)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>], dtype=int16)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.linspace(<span class="number">0</span>, <span class="number">10</span>, <span class="number">3</span>)</span><br><span class="line">array([  <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logspace(<span class="number">0</span>, <span class="number">9</span>, <span class="number">10</span>)                   <span class="comment"># 开始值是1，结束值是10^9，生成10个数</span></span><br><span class="line">array([  <span class="number">1.00000000e+00</span>,   <span class="number">1.00000000e+01</span>,   <span class="number">1.00000000e+02</span>,</span><br><span class="line">         <span class="number">1.00000000e+03</span>,   <span class="number">1.00000000e+04</span>,   <span class="number">1.00000000e+05</span>,</span><br><span class="line">         <span class="number">1.00000000e+06</span>,   <span class="number">1.00000000e+07</span>,   <span class="number">1.00000000e+08</span>,</span><br><span class="line">         <span class="number">1.00000000e+09</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.linspace(<span class="number">-10</span>,<span class="number">10</span>,<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.linspace(<span class="number">-10</span>,<span class="number">10</span>,<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x, y = np.meshgrid(x, y)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x</span><br><span class="line">array([[<span class="number">-10.</span>,  <span class="number">-5.</span>,   <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>],</span><br><span class="line">       [<span class="number">-10.</span>,  <span class="number">-5.</span>,   <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>],</span><br><span class="line">       [<span class="number">-10.</span>,  <span class="number">-5.</span>,   <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>],</span><br><span class="line">       [<span class="number">-10.</span>,  <span class="number">-5.</span>,   <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>],</span><br><span class="line">       [<span class="number">-10.</span>,  <span class="number">-5.</span>,   <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y</span><br><span class="line">array([[<span class="number">-10.</span>, <span class="number">-10.</span>, <span class="number">-10.</span>, <span class="number">-10.</span>, <span class="number">-10.</span>],</span><br><span class="line">       [ <span class="number">-5.</span>,  <span class="number">-5.</span>,  <span class="number">-5.</span>,  <span class="number">-5.</span>,  <span class="number">-5.</span>],</span><br><span class="line">       [  <span class="number">0.</span>,   <span class="number">0.</span>,   <span class="number">0.</span>,   <span class="number">0.</span>,   <span class="number">0.</span>],</span><br><span class="line">       [  <span class="number">5.</span>,   <span class="number">5.</span>,   <span class="number">5.</span>,   <span class="number">5.</span>,   <span class="number">5.</span>],</span><br><span class="line">       [ <span class="number">10.</span>,  <span class="number">10.</span>,  <span class="number">10.</span>,  <span class="number">10.</span>,  <span class="number">10.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.c_[<span class="number">0</span>:<span class="number">10</span>:<span class="number">3</span>]</span><br><span class="line">array([[<span class="number">0</span>],</span><br><span class="line">       [<span class="number">3</span>],</span><br><span class="line">       [<span class="number">6</span>],</span><br><span class="line">       [<span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.c_[<span class="number">0</span>:<span class="number">10</span>:<span class="number">3</span>].shape</span><br><span class="line">(<span class="number">4</span>, <span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.r_[<span class="number">0</span>:<span class="number">10</span>:<span class="number">3</span>]</span><br><span class="line">array([<span class="number">0</span>, <span class="number">3</span>, <span class="number">6</span>, <span class="number">9</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.r_[<span class="number">0</span>:<span class="number">10</span>:<span class="number">3</span>].shape</span><br><span class="line">(<span class="number">4</span>,)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.zeros(<span class="number">2</span>)</span><br><span class="line">array([ <span class="number">0.</span>,  <span class="number">0.</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.zeros((<span class="number">2</span>,<span class="number">3</span>))</span><br><span class="line">array([[ <span class="number">0.</span>,  <span class="number">0.</span>,  <span class="number">0.</span>],</span><br><span class="line">       [ <span class="number">0.</span>,  <span class="number">0.</span>,  <span class="number">0.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.zeros(<span class="number">2</span>, dtype=np.int16)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">0</span>], dtype=int16)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.ones((<span class="number">2</span>,<span class="number">3</span>))</span><br><span class="line">array([[ <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>],</span><br><span class="line">       [ <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.ones((<span class="number">2</span>,<span class="number">3</span>)) * <span class="number">8</span></span><br><span class="line">array([[ <span class="number">8.</span>,  <span class="number">8.</span>,  <span class="number">8.</span>],</span><br><span class="line">       [ <span class="number">8.</span>,  <span class="number">8.</span>,  <span class="number">8.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.zeros((<span class="number">2</span>,<span class="number">3</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.ones_like(arr, dtype=np.int16)</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>],</span><br><span class="line">       [<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>]], dtype=int16)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.empty((<span class="number">2</span>,<span class="number">3</span>))</span><br><span class="line">array([[  <span class="number">0.00000000e+000</span>,   <span class="number">0.00000000e+000</span>,   <span class="number">4.22764845e-307</span>],</span><br><span class="line">       [  <span class="number">3.47666793e-309</span>,   <span class="number">0.00000000e+000</span>,   <span class="number">0.00000000e+000</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.identity(<span class="number">3</span>)</span><br><span class="line">array([[ <span class="number">1.</span>,  <span class="number">0.</span>,  <span class="number">0.</span>],</span><br><span class="line">       [ <span class="number">0.</span>,  <span class="number">1.</span>,  <span class="number">0.</span>],</span><br><span class="line">       [ <span class="number">0.</span>,  <span class="number">0.</span>,  <span class="number">1.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.rand(<span class="number">5</span>) &gt; <span class="number">0.5</span></span><br><span class="line">array([ <span class="keyword">True</span>, <span class="keyword">False</span>,  <span class="keyword">True</span>,  <span class="keyword">True</span>, <span class="keyword">False</span>], dtype=bool)</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="使用切片和索引"><a href="#使用切片和索引" class="headerlink" title="使用切片和索引"></a>使用切片和索引</h3><ol>
<li><p>对于一维数组，操作与python原生数组差别不大</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[<span class="number">2</span>:<span class="number">4</span>]</span><br><span class="line">array([<span class="number">3</span>, <span class="number">4</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[<span class="number">-2</span>:]</span><br><span class="line">array([<span class="number">4</span>, <span class="number">5</span>])</span><br></pre></td></tr></table></figure>
</li>
<li><p>对于多维数组，如果使用python操作会很麻烦，各种循环。而对于numpy，非常简单</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([</span><br><span class="line"><span class="meta">... </span>    [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>],</span><br><span class="line"><span class="meta">... </span>    [<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>]</span><br><span class="line"><span class="meta">... </span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[:,<span class="number">1</span>]                            <span class="comment"># 获取第一列</span></span><br><span class="line">array([<span class="number">2</span>, <span class="number">7</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[:,<span class="number">2</span>:<span class="number">4</span>]                          <span class="comment"># 获取第二到四列</span></span><br><span class="line">array([[<span class="number">3</span>, <span class="number">4</span>],</span><br><span class="line">    [<span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>mask = np.array([                   <span class="comment"># 创建一个mask数组，就是一个bool类型的数组</span></span><br><span class="line"><span class="meta">... </span>    [<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>],</span><br><span class="line"><span class="meta">... </span>    [<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>]</span><br><span class="line"><span class="meta">... </span>], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[mask]                           <span class="comment"># 如果mask某个位置的值为true，那么就取arr数组对应的元素，否则就不取</span></span><br><span class="line">array([ <span class="number">2</span>,  <span class="number">6</span>,  <span class="number">8</span>, <span class="number">10</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>mask = np.random.rand(<span class="number">2</span>, <span class="number">5</span>) &gt; <span class="number">0.5</span>   <span class="comment"># 生成随机mask数组</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[mask]</span><br><span class="line">array([<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>])</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="数组运算"><a href="#数组运算" class="headerlink" title="数组运算"></a>数组运算</h3><h4 id="关于数值的计算"><a href="#关于数值的计算" class="headerlink" title="关于数值的计算"></a>关于数值的计算</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> numpy.sum</span><br><span class="line">    可以求所有元素的和，也可以按轴求和</span><br><span class="line"><span class="number">2.</span> numpy.prod</span><br><span class="line">    计算乘积</span><br><span class="line"><span class="number">3.</span> numpy.min/numpy.max</span><br><span class="line">    找最小/大值，可以拉通了找，也可以按轴找</span><br><span class="line"><span class="number">4.</span> numpy.argmin/numpy.argmax</span><br><span class="line">    找最小/大值所在的索引，可以拉通了找，也可以按轴找</span><br><span class="line"><span class="number">5.</span> numpy.mean</span><br><span class="line">    均值</span><br><span class="line"><span class="number">6.</span> numpy.std</span><br><span class="line">    标准差</span><br><span class="line"><span class="number">7.</span> numpy.var</span><br><span class="line">    方差</span><br><span class="line"><span class="number">8.</span> numpy.clip</span><br><span class="line">    限制，看代码</span><br><span class="line"><span class="number">9.</span> numpy.round</span><br><span class="line">    四舍五入，可以设置精度</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">数值计算实践</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([</span><br><span class="line"><span class="meta">... </span>    [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],</span><br><span class="line"><span class="meta">... </span>    [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line"><span class="meta">... </span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.sum()               <span class="comment"># 等价 np.sum(arr)</span></span><br><span class="line"><span class="number">21</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.sum(axis=<span class="number">0</span>)         <span class="comment"># 等价 np.sum(arr, axis=0) </span></span><br><span class="line">array([<span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.sum(axis=<span class="number">1</span>)         <span class="comment"># 等价 np.sum(arr, axis=1)</span></span><br><span class="line">array([ <span class="number">6</span>, <span class="number">15</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.prod()</span><br><span class="line"><span class="number">720</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.prod(axis=<span class="number">0</span>)</span><br><span class="line">array([ <span class="number">4</span>, <span class="number">10</span>, <span class="number">18</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.prod(axis=<span class="number">1</span>)</span><br><span class="line">array([  <span class="number">6</span>, <span class="number">120</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.min()</span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.min(axis=<span class="number">0</span>)</span><br><span class="line">array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.min(axis=<span class="number">1</span>)</span><br><span class="line">array([<span class="number">1</span>, <span class="number">4</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.max()</span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.max(axis=<span class="number">0</span>)</span><br><span class="line">array([<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.max(axis=<span class="number">1</span>)</span><br><span class="line">array([<span class="number">3</span>, <span class="number">6</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmax()            <span class="comment"># 这里是flat后的序号</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmax(axis=<span class="number">0</span>)</span><br><span class="line">array([<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmax(axis=<span class="number">1</span>)</span><br><span class="line">array([<span class="number">2</span>, <span class="number">2</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmin()            <span class="comment"># 这里是flat后的序号</span></span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmin(axis=<span class="number">0</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmin(axis=<span class="number">1</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">0</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.mean()</span><br><span class="line"><span class="number">3.5</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.mean(axis=<span class="number">0</span>)</span><br><span class="line">array([ <span class="number">2.5</span>,  <span class="number">3.5</span>,  <span class="number">4.5</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.mean(axis=<span class="number">1</span>)</span><br><span class="line">array([ <span class="number">2.</span>,  <span class="number">5.</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.std()</span><br><span class="line"><span class="number">1.707825127659933</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.std(axis=<span class="number">0</span>)</span><br><span class="line">array([ <span class="number">1.5</span>,  <span class="number">1.5</span>,  <span class="number">1.5</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.std(axis=<span class="number">1</span>)</span><br><span class="line">array([ <span class="number">0.81649658</span>,  <span class="number">0.81649658</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.var()</span><br><span class="line"><span class="number">2.9166666666666665</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.var(axis=<span class="number">0</span>)</span><br><span class="line">array([ <span class="number">2.25</span>,  <span class="number">2.25</span>,  <span class="number">2.25</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.var(axis=<span class="number">1</span>)</span><br><span class="line">array([ <span class="number">0.66666667</span>,  <span class="number">0.66666667</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.clip(<span class="number">2</span>,<span class="number">4</span>)                   <span class="comment"># 指定最大最小值，大于最大值的取最大值，小于最小值的取最小值</span></span><br><span class="line">array([[<span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">       [<span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.round(decimals=<span class="number">1</span>)</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">       [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]])</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="关于矩阵的计算"><a href="#关于矩阵的计算" class="headerlink" title="关于矩阵的计算"></a>关于矩阵的计算</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1. numpy.multiply</span><br><span class="line">    与运算符号*等价，表示乘法，对应位置分别相乘，一般很少用</span><br><span class="line">2. numpy.logical_&#123;and,or,xor,not&#125;</span><br><span class="line">    一系列的逻辑操作，并、或、异或、非</span><br><span class="line">3. numpy.dot</span><br><span class="line">    矩阵乘法运算，常用</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">矩阵计算实践</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.array([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]).reshape(<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.array([<span class="number">2</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]).reshape(<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">       [<span class="number">3</span>, <span class="number">4</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y</span><br><span class="line">array([[<span class="number">2</span>, <span class="number">4</span>],</span><br><span class="line">       [<span class="number">5</span>, <span class="number">6</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.multiply(x,y)            <span class="comment"># 与 x * y 等价</span></span><br><span class="line">array([[ <span class="number">2</span>,  <span class="number">8</span>],</span><br><span class="line">       [<span class="number">15</span>, <span class="number">24</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x * y</span><br><span class="line">array([[ <span class="number">2</span>,  <span class="number">8</span>],</span><br><span class="line">       [<span class="number">15</span>, <span class="number">24</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.array([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]).reshape(<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.array([<span class="number">2</span>,<span class="number">0</span>,<span class="number">5</span>,<span class="number">0</span>]).reshape(<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logical_and(x,y)</span><br><span class="line">array([[ <span class="keyword">True</span>, <span class="keyword">False</span>],</span><br><span class="line">       [ <span class="keyword">True</span>, <span class="keyword">False</span>]], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logical_or(x,y)</span><br><span class="line">array([[ <span class="keyword">True</span>,  <span class="keyword">True</span>],</span><br><span class="line">       [ <span class="keyword">True</span>,  <span class="keyword">True</span>]], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logical_xor(x,y)</span><br><span class="line">array([[<span class="keyword">False</span>,  <span class="keyword">True</span>],</span><br><span class="line">       [<span class="keyword">False</span>,  <span class="keyword">True</span>]], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logical_not(x)</span><br><span class="line">array([[<span class="keyword">False</span>, <span class="keyword">False</span>],</span><br><span class="line">       [<span class="keyword">False</span>, <span class="keyword">False</span>]], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logical_not(y)</span><br><span class="line">array([[<span class="keyword">False</span>,  <span class="keyword">True</span>],</span><br><span class="line">       [<span class="keyword">False</span>,  <span class="keyword">True</span>]], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.array([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]).reshape(<span class="number">1</span>,<span class="number">4</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.array([<span class="number">2</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]).reshape(<span class="number">4</span>,<span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.dot(x,y)                 <span class="comment"># 常用的矩阵相乘</span></span><br><span class="line">array([[<span class="number">49</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.dot(y, x)</span><br><span class="line">array([[ <span class="number">2</span>,  <span class="number">4</span>,  <span class="number">6</span>,  <span class="number">8</span>],</span><br><span class="line">       [ <span class="number">4</span>,  <span class="number">8</span>, <span class="number">12</span>, <span class="number">16</span>],</span><br><span class="line">       [ <span class="number">5</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">20</span>],</span><br><span class="line">       [ <span class="number">6</span>, <span class="number">12</span>, <span class="number">18</span>, <span class="number">24</span>]])</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="排序操作"><a href="#排序操作" class="headerlink" title="排序操作"></a>排序操作</h3><p>排序操作是数据分析过程中一个高频操作，这里看看如何使用numpy进行矩阵元素排序</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">1. ndarray.sort</span><br><span class="line">2. ndarray.argsort</span><br><span class="line">    不改变原始数组，返回排序后，新数据在原数据中的位置</span><br><span class="line">3. ndarray.searchsorted</span><br><span class="line">    在一个有序的序列中插入数据，算其插入位置</span><br><span class="line">4. numpy.lexsort</span><br><span class="line">    针对不同的列进行不同的排序</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">数组排序实践</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">5.6</span>, <span class="number">1.3</span>, <span class="number">7.5</span>, <span class="number">1.5</span>, <span class="number">7.8</span>, <span class="number">1.2</span>]).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.sort(axis=<span class="number">0</span>)            <span class="comment"># 按第一维排序，会修改原数组，如果不知道axis将拉平了后进行排序</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[ <span class="number">1.5</span>,  <span class="number">1.3</span>,  <span class="number">1.2</span>],</span><br><span class="line">       [ <span class="number">5.6</span>,  <span class="number">7.8</span>,  <span class="number">7.5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">5.6</span>, <span class="number">1.3</span>, <span class="number">7.5</span>, <span class="number">1.5</span>, <span class="number">7.8</span>, <span class="number">1.2</span>]).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[ <span class="number">5.6</span>,  <span class="number">1.3</span>,  <span class="number">7.5</span>],</span><br><span class="line">       [ <span class="number">1.5</span>,  <span class="number">7.8</span>,  <span class="number">1.2</span>]])</span><br><span class="line"><span class="comment"># 该函数将不会修改原数组，会返回排序后的数据在原始数组中的坐标，默认axis参数为1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argsort()</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">0</span>, <span class="number">2</span>],</span><br><span class="line">       [<span class="number">2</span>, <span class="number">0</span>, <span class="number">1</span>]])</span><br><span class="line"><span class="comment"># 想要将一系列值插入到一个有序的序列，但是不知道应该插在什么位置，searchsorted函数用于获取该位置索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.linspace(<span class="number">0</span>, <span class="number">9</span>, <span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([ <span class="number">0.</span>,  <span class="number">1.</span>,  <span class="number">2.</span>,  <span class="number">3.</span>,  <span class="number">4.</span>,  <span class="number">5.</span>,  <span class="number">6.</span>,  <span class="number">7.</span>,  <span class="number">8.</span>,  <span class="number">9.</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>values = np.array([<span class="number">2.5</span>, <span class="number">6.5</span>, <span class="number">9.5</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.searchsorted(arr, values)</span><br><span class="line">array([ <span class="number">3</span>,  <span class="number">7</span>, <span class="number">10</span>])</span><br><span class="line"><span class="comment"># 如果我们想要针对不同列排序，可以使用lexsort</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">1</span>, <span class="number">0</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">7</span>, <span class="number">0</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">0</span>]).reshape(<span class="number">4</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">0</span>, <span class="number">6</span>],</span><br><span class="line">       [<span class="number">1</span>, <span class="number">7</span>, <span class="number">0</span>],</span><br><span class="line">       [<span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>],</span><br><span class="line">       [<span class="number">2</span>, <span class="number">4</span>, <span class="number">0</span>]])</span><br><span class="line"><span class="comment"># 在保证index为2的列升序的情况下，index为0的列要实现降序</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index = np.lexsort([<span class="number">-1</span> * arr[:,<span class="number">0</span>], arr[:,<span class="number">2</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[index]</span><br><span class="line">array([[<span class="number">2</span>, <span class="number">4</span>, <span class="number">0</span>],</span><br><span class="line">       [<span class="number">1</span>, <span class="number">7</span>, <span class="number">0</span>],</span><br><span class="line">       [<span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>],</span><br><span class="line">       [<span class="number">1</span>, <span class="number">0</span>, <span class="number">6</span>]])</span><br></pre></td></tr></table></figure>

</div></div>
<h2 id="进阶"><a href="#进阶" class="headerlink" title="进阶"></a>进阶</h2><p>如果看到了这里，恭喜你，您已经非常棒了，看来您对numpy的兴趣还是非常强的，下面我们介绍一些高级点的内容</p>
<h3 id="数组形状的变更"><a href="#数组形状的变更" class="headerlink" title="数组形状的变更"></a>数组形状的变更</h3><ol>
<li><p>改变矩阵的维度</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.arange(<span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.shape = <span class="number">2</span>, <span class="number">5</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>],</span><br><span class="line">    [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.shape = <span class="number">5</span>, <span class="number">2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">    [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">    [<span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">    [<span class="number">6</span>, <span class="number">7</span>],</span><br><span class="line">    [<span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="comment"># 我们也可以直接使用reshape函数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.reshape(<span class="number">2</span>, <span class="number">5</span>)</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>],</span><br><span class="line">    [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="comment"># 将矩阵变成一个向量，拉平</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.flatten()</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.ravel()</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>])</span><br></pre></td></tr></table></figure>
</li>
<li><p>增加与删除维度</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.arange(<span class="number">10</span>).reshape(<span class="number">5</span>, <span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">    [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">    [<span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">    [<span class="number">6</span>, <span class="number">7</span>],</span><br><span class="line">    [<span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.shape</span><br><span class="line">(<span class="number">5</span>, <span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr = arr[np.newaxis, :, :]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr</span><br><span class="line">array([[[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">        [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">        [<span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">        [<span class="number">6</span>, <span class="number">7</span>],</span><br><span class="line">        [<span class="number">8</span>, <span class="number">9</span>]]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.shape</span><br><span class="line">(<span class="number">1</span>, <span class="number">5</span>, <span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr = arr[:, :, np.newaxis]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr</span><br><span class="line">array([[[<span class="number">0</span>],</span><br><span class="line">        [<span class="number">1</span>]],</span><br><span class="line"></span><br><span class="line">        [[<span class="number">2</span>],</span><br><span class="line">        [<span class="number">3</span>]],</span><br><span class="line"></span><br><span class="line">        [[<span class="number">4</span>],</span><br><span class="line">        [<span class="number">5</span>]],</span><br><span class="line"></span><br><span class="line">        [[<span class="number">6</span>],</span><br><span class="line">        [<span class="number">7</span>]],</span><br><span class="line"></span><br><span class="line">        [[<span class="number">8</span>],</span><br><span class="line">        [<span class="number">9</span>]]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.shape</span><br><span class="line">(<span class="number">5</span>, <span class="number">2</span>, <span class="number">1</span>)</span><br><span class="line"><span class="comment"># 删除多余没用的维度，也称为压缩操作</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.squeeze()</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">    [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">    [<span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">    [<span class="number">6</span>, <span class="number">7</span>],</span><br><span class="line">    [<span class="number">8</span>, <span class="number">9</span>]])</span><br></pre></td></tr></table></figure>
</li>
<li><p>获取转置矩阵</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 求转置矩阵</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.arange(<span class="number">10</span>).reshape(<span class="number">2</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>],</span><br><span class="line">    [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.transpose()</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">5</span>],</span><br><span class="line">    [<span class="number">1</span>, <span class="number">6</span>],</span><br><span class="line">    [<span class="number">2</span>, <span class="number">7</span>],</span><br><span class="line">    [<span class="number">3</span>, <span class="number">8</span>],</span><br><span class="line">    [<span class="number">4</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.T</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">5</span>],</span><br><span class="line">    [<span class="number">1</span>, <span class="number">6</span>],</span><br><span class="line">    [<span class="number">2</span>, <span class="number">7</span>],</span><br><span class="line">    [<span class="number">3</span>, <span class="number">8</span>],</span><br><span class="line">    [<span class="number">4</span>, <span class="number">9</span>]])</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="连接矩阵"><a href="#连接矩阵" class="headerlink" title="连接矩阵"></a>连接矩阵</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = (np.arange(<span class="number">6</span>) + <span class="number">1</span>).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">       [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = (np.arange(<span class="number">6</span>) + <span class="number">7</span>).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b</span><br><span class="line">array([[ <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.concatenate((a, b), axis=<span class="number">1</span>)</span><br><span class="line">array([[ <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>,  <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [ <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.concatenate((a, b), axis=<span class="number">0</span>)</span><br><span class="line">array([[ <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>],</span><br><span class="line">       [ <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>],</span><br><span class="line">       [ <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]])</span><br><span class="line"><span class="comment"># 如果待链接的矩阵是二维的，那么还可以用函数vstack和hstack</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.hstack((a, b))</span><br><span class="line">array([[ <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>,  <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [ <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.vstack((a, b))</span><br><span class="line">array([[ <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>],</span><br><span class="line">       [ <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>],</span><br><span class="line">       [ <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]])</span><br></pre></td></tr></table></figure>
<h3 id="随机模块"><a href="#随机模块" class="headerlink" title="随机模块"></a>随机模块</h3><p>随机值在机器学习中非常重要，在进行一个模型训练前都会选择一个随机值进行处理，这里我们一起学习一下吧</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1. numpy.random.rand</span><br><span class="line">    产生0到1的随机数，可以指定要产生矩阵的shape，默认只产生一个数</span><br><span class="line">2. numpy.random.random_sample</span><br><span class="line">    就产生一个随机数，与 numpy.random.rand()等价</span><br><span class="line">3. numpy.random.randint</span><br><span class="line">    用于产生整数，可以制定范围、个数以及shape</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">随机模块实践</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.rand()</span><br><span class="line"><span class="number">0.9083146428390426</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.rand(<span class="number">3</span>)</span><br><span class="line">array([ <span class="number">0.21180848</span>,  <span class="number">0.72211183</span>,  <span class="number">0.73075582</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.rand(<span class="number">2</span>, <span class="number">2</span>)</span><br><span class="line">array([[ <span class="number">0.66070728</span>,  <span class="number">0.3936634</span> ],</span><br><span class="line">       [ <span class="number">0.07193046</span>,  <span class="number">0.67754269</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.random_sample()</span><br><span class="line"><span class="number">0.6710709363984259</span></span><br><span class="line"><span class="comment"># 如果就一个整数，表示0到该参数内取整数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">10</span>)</span><br><span class="line"><span class="number">7</span></span><br><span class="line"><span class="comment"># 如果两个参数，表示最大最小值内取整数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">-10</span>, <span class="number">10</span>)</span><br><span class="line"><span class="number">-6</span></span><br><span class="line"><span class="comment"># 如果有size参数，则表示产生的矩阵shape</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">-10</span>, <span class="number">10</span>, size=(<span class="number">2</span>,<span class="number">2</span>))</span><br><span class="line">array([[ <span class="number">8</span>, <span class="number">-5</span>],</span><br><span class="line">       [<span class="number">-2</span>, <span class="number">-9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">10</span>, size=(<span class="number">2</span>,<span class="number">2</span>))</span><br><span class="line">array([[<span class="number">5</span>, <span class="number">9</span>],</span><br><span class="line">       [<span class="number">3</span>, <span class="number">5</span>]])</span><br><span class="line"><span class="comment"># 下面两种等价，产生一个向量</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">-10</span>, <span class="number">10</span>, size=(<span class="number">3</span>,))</span><br><span class="line">array([<span class="number">-3</span>, <span class="number">-6</span>,  <span class="number">9</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">-10</span>, <span class="number">10</span>, <span class="number">3</span>)</span><br><span class="line">array([<span class="number">-2</span>, <span class="number">-3</span>,  <span class="number">2</span>])</span><br><span class="line"><span class="comment"># 我们也可以产生满足高斯分布的向量</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>mu, sigma = <span class="number">0</span>, <span class="number">0.1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.normal(mu, sigma, <span class="number">10</span>)</span><br><span class="line">array([<span class="number">-0.10791302</span>,  <span class="number">0.08377314</span>,  <span class="number">0.03925479</span>, <span class="number">-0.06169874</span>,  <span class="number">0.06336937</span>,</span><br><span class="line">        <span class="number">0.08090765</span>, <span class="number">-0.19557721</span>, <span class="number">-0.0209661</span> , <span class="number">-0.02408436</span>,  <span class="number">0.19880881</span>])</span><br><span class="line"><span class="comment"># 我们可以使用shuffle函数打乱向量的顺序，只能是向量</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.arange(<span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.shuffle(arr)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([<span class="number">0</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">6</span>, <span class="number">4</span>])</span><br><span class="line"><span class="comment"># 计算机的随机数都是伪随机，有一个种子的概念，只要种子相同，产生的随机数就一定相同</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.seed(<span class="number">0</span>)</span><br></pre></td></tr></table></figure>

</div></div>
<h2 id="高阶"><a href="#高阶" class="headerlink" title="高阶"></a>高阶</h2><h3 id="文件读写"><a href="#文件读写" class="headerlink" title="文件读写"></a>文件读写</h3><p>文件读写也是我们经常使用的一个操作，从文件中读取文件，然后让numpy去处理</p>
<ol>
<li><p>读取一个文本文件</p>
<p> 可以自己手动写一个txt文件，如果使用anaconda，也可以通过 <code>%%writefile arr.txt</code>写一个文件</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8</span><br><span class="line">2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure>
<p> 这里我们比较一下，使用python读取文件与numpy读取文件的区别，你便可以了解到哪个更方便</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">######### 使用python #########</span></span><br><span class="line">data = []</span><br><span class="line"><span class="keyword">with</span> open(<span class="string">"arr.txt"</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> f.readlines():</span><br><span class="line">        fields = line.split()   <span class="comment"># 默认以空格分割</span></span><br><span class="line">        cur_data = [float(x) <span class="keyword">for</span> x <span class="keyword">in</span> fields]</span><br><span class="line">        data.append(cur_data)</span><br><span class="line">data = np.array(data)</span><br><span class="line"><span class="comment">######### 使用使用numpy #########</span></span><br><span class="line">data = np.loadtxt(<span class="string">'arr.txt'</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>读取一个文本文件（指定分隔符）</p>
<p> 上面的例子我们的文件是以空格作文分隔符区分每一个元素的，那如果我们的文件是逗号呢？</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 假设文件内容如下</span></span><br><span class="line"><span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span></span><br><span class="line"><span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span></span><br><span class="line"><span class="comment"># 我们需要指定逗号为分隔符</span></span><br><span class="line">data = np.loadtxt(<span class="string">'arr.txt'</span>, delimiter=<span class="string">','</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>读取一个文本文件（带有头信息）</p>
<p> 很多时候其实我们的文件可能不只是有数据内容，可能每列的头信息也标识出来了，像下面这样，如果还是使用上面的读取方式，又报错，那怎么办呢</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 假设文件内容如下</span><br><span class="line">a,b,c,d,e,f,g,h</span><br><span class="line">1,2,3,4,5,6,7,8</span><br><span class="line">2,3,4,5,6,7,8,9</span><br><span class="line"># 对于这种我们可以通过参数跳过某一列不读取</span><br><span class="line">data = np.loadtxt(&apos;arr.txt&apos;, delimiter=&apos;,&apos;, skiprows=1)     # 这里的参数skiprows=1，前面的1行都不读取，如果设置5，表示前面5行都不读取</span><br></pre></td></tr></table></figure>
</li>
<li><p>读取一个文本文件（只读取指定的列）</p>
<p> 很多时候文件记录的内容很多，但是我们需要的可能就某几列而已，那么可以用如下的方式指定列</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 只读取0，2，3列的内容</span></span><br><span class="line">data = np.loadtxt(<span class="string">'arr.txt'</span>, delimiter=<span class="string">','</span>, skiprows=<span class="number">1</span>, usecols=(<span class="number">0</span>,<span class="number">2</span>,<span class="number">3</span>))</span><br></pre></td></tr></table></figure>
</li>
<li><p>上面4个点我们讨论的都是怎么去读取一个文本文件，那我们是否有方式将我们的数组保存到文件呢，当然有啦</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">data = np.array([</span><br><span class="line">    [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],</span><br><span class="line">    [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line">])</span><br><span class="line">np.savetxt(<span class="string">'myarr.txt'</span>, data, delimiter=<span class="string">','</span>)</span><br><span class="line"><span class="comment"># 去看看文件，我们发现写入的内容都是科学计数法的，比较大，能否简化呢？</span></span><br><span class="line">np.savetxt(<span class="string">'myarr.txt'</span>, data, delimiter=<span class="string">','</span>, fmt=<span class="string">'%d'</span>)  <span class="comment"># 添加fmt参数指定保存的格式，%d 表示整数，也可以用其他的，比如 %.2f 有两位小数的浮点数</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>我们的数据都保存成了txt文件，是可以打开看到内容，其实我们的数据只要程序能看得懂就可以了，我们看不看得无所谓，因此一般也会将这种数据直接保存为二进制格式，节省空间</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">data = np.array([</span><br><span class="line">    [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],</span><br><span class="line">    [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line">])</span><br><span class="line">np.save(<span class="string">'myarr.npy'</span>, data)  <span class="comment"># 执行完了后，我们能在当前目录看到 myarr.npy 的文件，但是打开是看不懂内容的，这就是二进制格式存储的</span></span><br><span class="line">data = np.load(<span class="string">'myarr.npy'</span>) <span class="comment"># 读取也是非常简单，使用load方法即可</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>第6点我们讲到了如何将数组保存到一个二进制文件中，我们的例子只是一个数组，那要是多个数组呢？应该怎么保存于读取呢？</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">x = np.arange(<span class="number">0</span>,<span class="number">10</span>,<span class="number">1</span>)</span><br><span class="line">y = np.arange(<span class="number">10</span>,<span class="number">20</span>,<span class="number">1</span>)</span><br><span class="line">np.savez(<span class="string">'myarr.npz'</span>, x=x, y=y) <span class="comment"># 使用savez函数将多个数组保存到一个npz格式的文件中，x，y都是自己写的，也可以写a，b等等</span></span><br><span class="line">data = np.load(<span class="string">'myarr.npz'</span>)     <span class="comment"># 读取还是使用的load函数，没有什么不同，只是如果读取的文件是多个数组压缩保存的文件，那load函数返回的不是一个数组，而是一个数组集合，使用方式如下</span></span><br><span class="line"></span><br><span class="line">data.keys()                     <span class="comment"># 获取keys</span></span><br><span class="line">data[<span class="string">'x'</span>]                       <span class="comment"># 通过这种方式读取对应的数组内容</span></span><br><span class="line">data[<span class="string">'y'</span>]</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="其他常规选项"><a href="#其他常规选项" class="headerlink" title="其他常规选项"></a>其他常规选项</h2><ol>
<li>设置浮点数据的显示精度</li>
</ol>
<p>注意这里只是显示上设置了，原始数据并没有改变，主要方便我们查看</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">np.set_printoptions(precision=<span class="number">3</span>)        <span class="comment"># 设置显示的精度</span></span><br></pre></td></tr></table></figure>
<h2 id="练习题"><a href="#练习题" class="headerlink" title="练习题"></a>练习题</h2><p>计算机是一门动手的科学，想要学好，必须实践，相信你可以的</p>
<ol>
<li>打印当前Numpy版本</li>
<li>构造一个全零的矩阵，并打印其占用的内存大小</li>
<li>打印一个函数的帮助文档，比如numpy.add</li>
<li>创建一个10-49的数组，并将其倒序排序</li>
<li>找到一个数组中不为0的索引</li>
<li>随机构造一个3*3矩阵，并打印其中最大与最小值</li>
<li>构造一个5*5的矩阵，令其值都为1，并在最外层加上一圈0</li>
<li>构建一个shape为(6,7,8)的矩阵，并找到第100个元素的索引值</li>
<li>对一个5*5的矩阵做归一化操作</li>
<li>找到两个数组中相同的值</li>
<li>得到今天、明天、昨天的日期</li>
<li>得到一个月中所有的天</li>
<li>得到一个数的整数部分</li>
<li>构造一个数组，让它不能被改变</li>
<li>打印大数据的部分值，全部值</li>
<li>找到在一个数组中，最接近一个数的索引</li>
<li>32位float类型和32位int类型转换</li>
<li>打印数组元素位置坐标与数值</li>
<li>按照数组的某一列进行排序</li>
<li>统计数组中每个数值出现的次数</li>
<li>如何对一个四维数组的最后两维来求和</li>
<li>交换矩阵中的两行</li>
<li>找到一个数组中最常出现的数字</li>
<li>快速查找TOP K</li>
<li>去除掉一个数组中，所有元素都相同的数据</li>
</ol>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/numpy/" rel="tag"># numpy</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/09/04/1.html" rel="next" title="k8s dashboard 源码分析（一）">
                <i class="fa fa-chevron-left"></i> k8s dashboard 源码分析（一）
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/09/15/2.html" rel="prev" title="数据分析之pandas">
                数据分析之pandas <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpg"
                alt="jackstraw" />
            
              <p class="site-author-name" itemprop="name">jackstraw</p>
              <p class="site-description motion-element" itemprop="description">人生的意义，不在于最终获得了什么，而在于曾经努力追求过什么</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#NumPy"><span class="nav-number">1.</span> <span class="nav-text">NumPy</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#基础"><span class="nav-number">1.1.</span> <span class="nav-text">基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#ndarray常用属性"><span class="nav-number">1.1.1.</span> <span class="nav-text">ndarray常用属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ndarray常用方法"><span class="nav-number">1.1.2.</span> <span class="nav-text">ndarray常用方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建ndarray数组"><span class="nav-number">1.1.3.</span> <span class="nav-text">创建ndarray数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用切片和索引"><span class="nav-number">1.1.4.</span> <span class="nav-text">使用切片和索引</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数组运算"><span class="nav-number">1.1.5.</span> <span class="nav-text">数组运算</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#关于数值的计算"><span class="nav-number">1.1.5.1.</span> <span class="nav-text">关于数值的计算</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关于矩阵的计算"><span class="nav-number">1.1.6.</span> <span class="nav-text">关于矩阵的计算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#排序操作"><span class="nav-number">1.1.7.</span> <span class="nav-text">排序操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#进阶"><span class="nav-number">1.2.</span> <span class="nav-text">进阶</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#数组形状的变更"><span class="nav-number">1.2.1.</span> <span class="nav-text">数组形状的变更</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#连接矩阵"><span class="nav-number">1.2.2.</span> <span class="nav-text">连接矩阵</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#随机模块"><span class="nav-number">1.2.3.</span> <span class="nav-text">随机模块</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#高阶"><span class="nav-number">1.3.</span> <span class="nav-text">高阶</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#文件读写"><span class="nav-number">1.3.1.</span> <span class="nav-text">文件读写</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#其他常规选项"><span class="nav-number">1.4.</span> <span class="nav-text">其他常规选项</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#练习题"><span class="nav-number">1.5.</span> <span class="nav-text">练习题</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">jackstraw</span>

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>访问人数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: true,
        notify: false,
        appId: 'lQM75w94ggNR0TjX61NLerrg-gzGzoHsz',
        appKey: 'I2wtQ2rd9KtoJmcEOiYG9zqT',
        placeholder: '如需帮助，请留下邮箱',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
    var infoEle = document.querySelector('#comments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0){
      infoEle.childNodes.forEach(function(item) {
        item.parentNode.removeChild(item);
      });
    }
  </script>



  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("lQM75w94ggNR0TjX61NLerrg-gzGzoHsz", "I2wtQ2rd9KtoJmcEOiYG9zqT");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
